/*
package com.yaoyao;


import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import javax.xml.bind.SchemaOutputResolver;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ESTest {
    TransportClient client = null;
    //创建索引库
   */
/* @Test
    public void creationIndice()throws Exception{
       //创建setting对象
        Settings settings = Settings.builder().put("cluster.name","my-elasticsearch").build();
        //创建客户端Client对象
    tran = new PreBuiltTransportClient(settings);
      tran.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
        tran.admin().indices().prepareCreate("yaoyao2").get();
        tran.close();

    }*//*


    @Before //初始化
    public void before()throws Exception{
      //  System.out.println("before");
        //创建setting对象
        Settings settings = Settings.builder().put("cluster.name","myelasticsearch").build();
        //创建客户端Client对象
        client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

    }
    @After //最后执行的操作
    public void after(){
        client.close();
        // System.out.println("after");

    }
    public void read( SearchResponse searchResponse){
        if(searchResponse!=null){
            SearchHits hits = searchResponse.getHits();
            if (hits!=null){
                SearchHit[] hits1 = hits.getHits();
                if (hits1!=null && hits1.length>0){
                    for (SearchHit searchHitFields : hits1) {
                        Map<String, Object> source = searchHitFields.getSource();
                        System.out.println(source);
                    }
                }
            }
        }
    }
    @Test//创建索引库
    public void creationIndice()throws Exception{
       //获取client对象进行操作
        client.admin().indices().prepareCreate("yaoyao1").get();
    }
    @Test//设置mapping映射
    public void setMapping()throws Exception{
        //先创建mapping字符串或者实体类对象或者XContextBuilder对象或者json对象
        String mapping =" {\n" +
                "        \"doc\": {\n" +
                "            \"properties\": {\n" +
                "                \"id\": {\n" +
                "                \t\"type\": \"long\",\n" +
                "                    \"store\": true,\n" +
                "                    \"index\":\"not_analyzed\"\n" +
                "                },\n" +
                "                \"title\": {\n" +
                "                \t\"type\": \"text\",\n" +
                "                    \"store\": true,\n" +
                "                    \"index\":\"analyzed\",\n" +
                "                    \"analyzer\":\"standard\"\n" +
                "                },\n" +
                "                \"content\": {\n" +
                "                \t\"type\": \"text\",\n" +
                "                    \"store\": true,\n" +
                "                    \"index\":\"analyzed\",\n" +
                "                    \"analyzer\":\"standard\"\n" +
                "                }\n" +
                "            }\n" +
                "        }\n" +
                "    }";
        //获取client对象进行操作
//client.prepareIndex("yaoyao3","doc").setSource(mapping, XContentType.JSON).get();
    client.admin().indices().preparePutMapping("yaoyao3").setType("doc").setSource(mapping,XContentType.JSON).get();//执行

    }
    @Test//添加文档
    public void creationDocument()throws Exception{
        Map map = new HashMap();
        map.put("id","1");
        map.put("title","你好漂亮");
        map.put("content","你真漂亮");
        //获取client对象进行操作
        client.prepareIndex("yaoyao3","doc").setSource(map).get();
    }
    @Test//批量创建索引库
    public void creationDocument2()throws Exception{

        for (int i = 2; i <20 ; i++) {
            Map map = new HashMap();

            map.put("id",String.valueOf(i));
            map.put("title","你好漂亮"+i);
            map.put("content","你真漂亮"+i);
            client.prepareIndex("yaoyao3","doc").setSource(map).get();
        }

    }
    @Test//通过termQuery查询 不会对关键字进行任何操作,直接去词库搜索
    public void queryById()throws Exception{
        //获取client对象进行操作
        SearchResponse searchResponse = client.prepareSearch("yaoyao3").setTypes("doc")
                .setQuery(QueryBuilders.termQuery("id", "3")).get();
        read(searchResponse);

    }
    @Test//通过queryStringQuery查询 会对关键字进行分词然后搜索 不管查多少默认分页10条数据
    public void queryById2()throws Exception{
        //获取client对象进行操作
        SearchResponse searchResponse = client.prepareSearch("yaoyao3")
                .setTypes("doc").setQuery(QueryBuilders.queryStringQuery("漂亮").defaultField("title")).get();
        read(searchResponse);
    }
    @Test//通过idQuery查询 会对关键字进行分词然后搜索 不管查多少默认分页10条数据
    public void queryById3()throws Exception{
        //获取client对象进行操作
        SearchResponse searchResponse = client.prepareSearch("yaoyao3")
                .setTypes("doc").setQuery(QueryBuilders.idsQuery().addIds("1","2")).get();
        read(searchResponse);
    }
    //通过wildcardQuery查询 会对关键字进行分词然后搜索 不管查多少默认分页10条数据
    @Test//设置分页
    public void queryById4()throws Exception{
        SearchResponse searchResponse = client.prepareSearch("yaoyao3").setQuery(QueryBuilders.queryStringQuery("漂亮"))
                .setFrom(0)//设置分页相当于x 从哪里开始
                .setSize(5)//设置查询的条数
                .get();
        read(searchResponse);
    }

}
*/
